<template>
  <van-button
    :icon="value ? 'star' : 'star-o'"
    :class="{
      collected: value,
    }"
    :loading="loading"
    @click="onCollect"
  >
  </van-button>
</template>

<script>
import { deleteCollect, addCollect } from "@/api/atricle";
import { stringify } from "json-bigint";
export default {
  data() {
    return {
      loading: false,
    };
  },
  props: {
    //  通过父组件的v-model传值给子组件,从而子组件收到一个value的自定义属性和监听自定义属性Value变化的input事件,从而实现对父组件数据的获取和修改
    value: {
      type: Boolean,
      required: true,
    },
    articleId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  methods: {
    async onCollect() {
        this.loading=true
        console.log(11)
         console.log(this.articleId)
      try {
        if (this.value) {
          // 如果已经收藏了则取消
          await deleteCollect(this.articleId);
        } else {
          await addCollect(this.articleId);
        }
        this.loading=false
        // 更新视图
        this.$emit('input',!this.value)
        this.$toast.success(!this.value ? "收藏成功":"取消收藏")
      } catch (err) {
         this.$toast.fail('操作失败,稍后再试') 
         console.log(err)
      }
    },
  },
};
</script>

<style lang='less' scoped>
.collected {
  .van-icon {
    color: #ffa500;
  }
}
</style>